# 查看商品信息
# 保存用户信息
# 添加购物车
# 结算

# 购物系统类
# 实例属性 -- 商品信息、系统说明信息、 当前用户--多个用户
# 方法： 登录、 注册、 查看商品、添加购物车、结算

# 用户
# 实例属性： 名字 、 密码、 钱、 --购物车

class User:
    # 用户的姓名密码和初始金额
    def __init__(self, name, passwd, init_amount):
        self.name = name
        self.passwd = passwd
        self.init_amount = init_amount
        self.shopping_car = {}

class ShopingSystem:
    # 购物系统的信息
    def __init__(self, goods_info, sys_info):
        self.goods_info = goods_info
        self.sys_info = sys_info
        self.user_dict = {}

    # 登录
    def register(self, name, passwd, initial_amout):
        if name not in self.user_dict:
            # 创建一个新用户,实例化User类
            # user = User(name, passwd, init_amount)
            # 把名字作为key, 把user对象作为value 存在self.user_dict里面
            # self.user_dict[name] = {"passwd": passwd, "initial_amout": initial_amout}
            self.user_dict[name] = {"passwd": passwd, "initial_amout": initial_amout}
        else:
            print("用户已存在")

fruit_goods = {
        "F01": {"name": "apple", "price": 1},
        "F02": {"name": "banana", "price": 2},
        "F03": {"name": "pear", "price": 3}
}

drugstore = {
    "M01": {"name": "奥司他韦", "price": 60},
    "M02": {"name": "抗病毒口服液", "price": 30}

}
# 不同的购物系统
system1 = ShopingSystem(fruit_goods, "水果购物系统")

system2 = ShopingSystem(drugstore, "药店购物系统")

system = [system1, system2]




while 1:
    print("当前平台已有购物系统如下：")
    for k, v in enumerate(system):
        print(f"{k}.{v.sys_info}")
    c1 = input("请输入你的选择（按q退出）：")
    if c1 == "q":
        break
    # 判断选择是否存在
    if c1.isdigit() and int(c1) < len(system):
        # 转换为整型，方便后面用下标获取
        c1 = int(c1)
        # 当前的购物系统
        current_system = system[c1]
        print(f"欢迎进入{current_system.sys_info}".center(40, "*"))
        # while 1:
        c2 = input("1、登录\n2、注册\n请输入你的选择（按Q退出）")
        if c2 == "q":
            break
        if c2 == "1":
            # pass
            user_name = input("请输入用户名:")
            login_passwd = input("密码:")
            # 判断用户名是否存在当前系统里，不存在则跳回上一级，重新选择登录或者是注册
            if user_name not in current_system.user_dict.keys():
                print("用户名不存在，请重新输入或注册")
                break
            elif login_passwd == current_system.user_dict[user_name].get("passwd"):
                print("登录成功")
                print(f"欢迎用户{user_name},您的初始金额为{init_amount}")
                # 登录后
                while 1:
                    c3 = input("1、查看商品信息\n2、添加商品到购物车\n3、查看购物车\n4、结算\nq退出")
                    if c3 == "1":
                        print(current_system.goods_info)
                        c4 = input("1、添加商品到购物车\n2、查看购物车\nq退出")
                        if c4 == "1":
                            c5 = input("请输入商品编号")
                            if c5 in current_system.goods_info.keys():
                                user.shopping_car[c5] = current_system.goods_info[c5]
                            else:
                                print("请输入正确的商品编号")
                    elif c3 == "2":
                        print("添加购物车")
                    elif c3 == "3":
                        print("查看购物车")
                    elif c3 == "4":
                        print("结算")
                    elif c3 == "q":
                        break

        elif c2 == "2":
            name, passwd, init_amount = input("请输入你的名字，密码，和初始金额（空白分割）").split()
            user = User(name, passwd, init_amount)
            current_system.register(name, passwd, init_amount)
            print(f"当前系统用户有{current_system.user_dict.keys()}")
        else:
            print("输入有误")
    else:
        print("输入有误，请重新输入")



    #             while 1:
    #                 c3 = input("1、查看商品信息\n2、添加商品到购物车\n3、查看购物车\nq退出")
    #                 if c3 == "1":
    #                     print(current_system.goods_info)
    #                     c4 = input("1、添加商品到购物车\n2、查看购物车\nq退出")
    #                     if c4 == "1":
    #                         c5 = input("请输入商品编号")
    #                         if c5 in current_system.goods_info.keys():
    #                             user.shopping_car[c5] = current_system.goods_info[c5]
    #                         else:
    #                             print("请输入正确的商品编号")
    #                 elif c3 == "2":
    #                     print("添加购物车")
    #
    #         elif c2 == "2":
    #             name, passwd, init_amount = input("请输入你的名字，密码，和初始金额（空白分割）").split()
    #             user = User(name, passwd, init_amount)
    #             current_system.register(name, passwd, init_amount)
    #             print(f"当前系统用户有{current_system.user_dict.keys()}")
    #         else:
    #             print("输入有误")
    # else:
    #     print("输入有误，请重新输入")




